package com.zkflzl.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkflzl.store.common.BaseResponse;
import com.zkflzl.store.common.ErrorCode;
import com.zkflzl.store.common.ResultUtils;
import com.zkflzl.store.exception.ThrowUtils;
import com.zkflzl.store.mapper.ShopStockMapper;
import com.zkflzl.store.model.dto.shopstock.ShopStockAddGoodsRequest;
import com.zkflzl.store.model.dto.shopstock.ShopStockAddRequest;
import com.zkflzl.store.model.dto.shopstock.ShopStockListRequest;
import com.zkflzl.store.model.entity.ShopStock;
import com.zkflzl.store.model.entity.User;
import com.zkflzl.store.service.ShopStockService;
import com.zkflzl.store.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

import static com.zkflzl.store.constant.UserConstant.ADMIN_ROLE;

/**
 * @author 21582
 * @description 针对表【tb_shop_stock(门店库存表)】的数据库操作Service实现
 * @createDate 2024-03-20 09:40:34
 */
@Service
public class ShopStockServiceImpl extends ServiceImpl<ShopStockMapper, ShopStock>
        implements ShopStockService {
    @Resource
    private UserService userService;

    @Override
    public BaseResponse addShopStockGoods(ShopStockAddGoodsRequest shopStockAddGoodsRequest, HttpServletRequest request) {

        User user = userService.getLoginUser(request);

        Long storeId = shopStockAddGoodsRequest.getStoreId();
        Long shopId = shopStockAddGoodsRequest.getShopId();
        String name = shopStockAddGoodsRequest.getName();
        Integer quantity = shopStockAddGoodsRequest.getQuantity();

        boolean blank = StringUtils.isAnyBlank(storeId.toString(), shopId.toString(), name);
        ThrowUtils.throwIf(!blank, ErrorCode.PARAMS_ERROR);

        // 权限判断  非管理员、店员
        ThrowUtils.throwIf(!Objects.equals(user.getUserRole(), ADMIN_ROLE) ||
                !Objects.equals(user.getShopId(), shopId), ErrorCode.NO_AUTH_ERROR);


        ShopStock shopStock = new ShopStock();
        shopStock.setUserId(user.getId());
        shopStock.setStoreId(storeId);
        shopStock.setShopId(shopId);
        shopStock.setName(name);
        shopStock.setQuantity(quantity);

        // 保存
        boolean save = this.save(shopStock);
        ThrowUtils.throwIf(save, ErrorCode.OPERATION_ERROR);

        return ResultUtils.success("新增成功");
    }

    @Override
    public BaseResponse updateShopStock(ShopStockAddRequest stockAddRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);

        Long storeId = stockAddRequest.getStoreId();
        Long shopId = stockAddRequest.getShopId();
        Integer quantity = stockAddRequest.getQuantity();


        boolean blank = StringUtils.isAnyBlank(shopId.toString(), storeId.toString(), quantity.toString());
        ThrowUtils.throwIf(blank, ErrorCode.PARAMS_ERROR);

        // 判断门店是否存在该库存商品
        QueryWrapper<ShopStock> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("storeId", storeId)
                .eq("shopId", shopId);
        ShopStock shopStock = this.getOne(queryWrapper);
        if (shopStock == null) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR, "库存商品不存在");
        }


        UpdateWrapper<ShopStock> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq("shopId", shopId)
                .eq("storeId", storeId)
                .setSql("quantity = quantity + " + quantity);

        boolean update = this.update(updateWrapper);
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);

        return ResultUtils.success("添加库存成功");
    }

    @Override
    public BaseResponse delShopStock(String storeId, HttpServletRequest request) {
        User user = userService.getLoginUser(request);

        ThrowUtils.throwIf(StringUtils.isBlank(storeId), ErrorCode.PARAMS_ERROR);


        QueryWrapper<ShopStock> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("shopId", user.getShopId())
                .eq("storeId", storeId);
        boolean remove = this.remove(queryWrapper);
        ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR);

        return ResultUtils.success("删除库存商品成功");
    }

    @Override
    public BaseResponse selShopStock(ShopStockListRequest shopStockListRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);

        String shopId = shopStockListRequest.getShopId();
        String storeId = shopStockListRequest.getStoreId();
        long current = shopStockListRequest.getCurrent();
        long pageSize = shopStockListRequest.getPageSize();
        String sortField = shopStockListRequest.getSortField();
        String sortOrder = shopStockListRequest.getSortOrder();

        if (StringUtils.isBlank(shopId)) {
            shopId = user.getShopId().toString();
        }

        QueryWrapper<ShopStock> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(sortField) && !StringUtils.isBlank(sortOrder)) {
            queryWrapper.orderBy(true, "asc".equals(sortOrder), sortField);
        }
        if (!StringUtils.isBlank(storeId)) {
            queryWrapper.eq("storeId", storeId);
        }
        queryWrapper.eq("shopId", shopId);

        Page<ShopStock> page = new Page<>(current, pageSize);
        Page<ShopStock> shopStockPage = this.page(page, queryWrapper);
        return ResultUtils.success(shopStockPage);
    }
}




